package com.example.exam.dao;

import com.example.exam.entity.Exam;
import com.example.exam.util.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * 考试数据访问对象
 */
public class ExamDAO {
    private QueryRunner queryRunner = new QueryRunner();
    
    // 获取学生的所有可用考试
    public List<Exam> getAvailableExams(String studentId) {
        // 这里简化处理，实际上可能需要根据学生的课程关联来获取考试
        String sql = "SELECT * FROM exams WHERE status = '已激活' AND start_time <= ? AND end_time >= ?";
        try (Connection conn = DBUtil.getConnection()) {
            return queryRunner.query(conn, sql, new BeanListHandler<>(Exam.class), new Date(), new Date());
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }
    
    // 获取考试详情
    public Exam getExamById(int examId) {
        String sql = "SELECT * FROM exams WHERE exam_id = ?";
        try (Connection conn = DBUtil.getConnection()) {
            return queryRunner.query(conn, sql, new BeanHandler<>(Exam.class), examId);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }
}    